// ==UserScript==
// @name         自动答题
// @namespace    http://tampermonkey.net/
// @version      2024-04-01
// @description  try to take over the world!
// @author       You
// @match        https://learning.mhtall.com/rest/course/exercise/item/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=mhtall.com
// @grant        none
// ==/UserScript==

(function () {

    /**
     * 弹出消息
     * @param msg 消息
     * @param type 类型
     * @param time
     */
    function msg(msg, type, time) {
        console.log(msg)
        let htmlDivElement = document.createElement("div-my-msg");
        htmlDivElement.innerHTML = msg;
        htmlDivElement.style.position = "fixed";
        htmlDivElement.style.font = "bold 20px Arial";
        htmlDivElement.style.left = "50%";
        htmlDivElement.style.top = "20%";
        htmlDivElement.style.zIndex = "9999";
        htmlDivElement.style.color = type || 'red';
        time = time || 3000;
        if (document.getElementsByTagName("div-my-msg").length > 1) {
            setTimeout(
                function () {
                    document.getElementsByTagName("div-my-msg")[0].remove();
                }, 100
            )
        }
        document.body.appendChild(htmlDivElement);
        setTimeout(function () {
            htmlDivElement.remove();
        }, time);
    }

    function getRandomInt(min, max) {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    let rand = getRandomInt(1000, 2600);

    /**
     * 展示答案
     */
    function showAnswer() {
        document.getElementsByClassName("button_short")[0].click();//点击查看答案
    }

    /**
     * 解析答案
     * @returns {string[]}
     */
    function parseAnswer() {
        let content = document.getElementsByClassName("div_answer")[0].innerHTML;
        let answers = content.match(/[A-Z]/g);
        answers = answers.map(function (item) {
            return item.charCodeAt(0) - 65;
        })
        return answers;
    }

    /**
     * 选择答案
     * @param answer
     */
    function selectAnswer(answers) {
        let ipt_rdo_option = document.getElementsByClassName("ipt_rdo_option");
        let ipt_chk_option = document.getElementsByClassName("ipt_chk_option");
        for (let i = 0; i < answers.length; i++) {
            setTimeout(function () {
                if (ipt_chk_option.length > 0) {
                    if (ipt_chk_option[answers[i]].checked === false) {
                        ipt_chk_option[answers[i]].click();
                    }
                } else {
                    if (ipt_rdo_option[answers[i]].checked === false) {
                        ipt_rdo_option[answers[i]].click();
                    }
                }
            }, rand)
        }
    }

    /**
     * 下一题
     */
    function next() {
        setTimeout(function () {
            document.getElementsByClassName("button_short")[2].click();
        }, rand);
    }

    /**
     * 获取题号
     * @returns {string}
     */
    function getQuestionNum() {
        let href = window.location.href;
        let index = href.lastIndexOf("/");
        return href.substring(index + 1);
    }

    function submit() {
        let length = document.getElementsByClassName("button_short").length;
        console.log(length)
        let numberQuestions = length - 3;//减去了 查看答案，上一题，下一题  三个按钮
        let questionNum = getQuestionNum();
        if (numberQuestions == questionNum) {
            finish()//调用了页面中的finish 方法

            let dialog = document.getElementsByClassName("layui-m-layerbtn");
            dialog.item(0).children[1].click();
        }
    }

    function refresh() {
        window.location.reload();
    }

    setTimeout(async function () {
        await showAnswer();
        let answers = parseAnswer();
        await selectAnswer(answers);
        await next();
        await submit();
    }, rand)


})();
